package com.iaiai.tm.activity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 
 * <p>
 * Title: ApplicationListView.java
 * </p>
 * <p>
 * E-Mail: 176291935@qq.com
 * </p>
 * <p>
 * QQ: 176291935
 * </p>
 * <p>
 * Http: iaiai.iteye.com
 * </p>
 * <p>
 * Create time: 2012-2-9 下午3:33:59
 * </p>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public class ApplicationListView extends ListView {

	private Context context;
	
	private List<String> list;
	
	private Adapter adapter;

	/**
	 * @param context
	 * @param attrs
	 */
	public ApplicationListView(Context context, AttributeSet attrs) {
		super(context, attrs);

		this.context = context;
		
		init();
	}

	/**
	 * @param context
	 */
	public ApplicationListView(Context context) {
		super(context);

		this.context = context;
		
		init();
	}
	
	private void init(){
		list = getData();
		
		adapter = new Adapter();
		this.setAdapter(adapter);
	}

	/**
	 * 返回所有运行的程序
	 * 
	 * @return
	 */
	private List<String> getData() {
		List<String> l = new ArrayList<String>();
		readData();
		return l;
	}
	
	private void readData(){
		final Handler handler = new Handler();
		new Thread() {
			public void run() {
				ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
				final List l = am.getRunningAppProcesses();
				handler.post(new Runnable() {
					public void run() {
						Iterator i = l.iterator();
						PackageManager pm = context.getPackageManager();
						while (i.hasNext()) {
							ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) (i.next());
							try {
								CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName,
										PackageManager.GET_META_DATA));
								list.add(c.toString());
							} catch (Exception e) {
							}
						}
						adapter.notifyDataSetChanged();
					}
				});
			}
		}.start();
	}
	
	class Adapter extends BaseAdapter {  

	    public int getCount() {  
	        return list.size();  
	    }  
	  
	    public Object getItem(int position) {  
	        return list.get(position);  
	    }  
	  
	    public long getItemId(int position) {  
	        return position;  
	    }  
	  
	    public View getView(final int position, View convertView, ViewGroup parent) { 
	    	LinearLayout layout = new LinearLayout(context);
	    	layout.setPadding(5, 0, 0, 0);
	    	TextView textView = new TextView(context);
	    	textView.setTextColor(Color.BLACK);
	    	textView.setText(list.get(position));
	    	layout.addView(textView);
	        return layout;
	    }
	}

}
